package com.cn.jasyptdbtwo.config;

import com.cn.jasyptdbtwo.util.ByteUtil;
import com.cn.jasyptdbtwo.util.SM2Util;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO  处理解密
 *  此处的理解是：
 *  在配置文件中将敏感数据进行加密，
 *  在运行启动的时候，在此处进行解密，然后重新写入到配置文件中
 *  此处解密可使用自定义的加解密方法
 *  EnvironmentPostProcessor可以在创建应用程序上下文之前，添加或者修改环境配置。
 *
 * @ClassName EncryptEnvironmentPostProcessor
 * @Author xiaojiang
 * @Date 2021/9/23 下午 12:17
 */
public class EncryptEnvironmentPostProcessor   implements EnvironmentPostProcessor  {

    private static final String PWD_PREFIX = "sun(" ;
    private static final String PWD_SUFFIX = ")" ;
    private static final String DS_KEY = "spring.datasource.password" ;
    BigInteger PrivateKey = new BigInteger("45666848317442077248920409648826016378377386665000297387948515223319986101367");

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        System.out.println("准备处理加解密数据") ;
        String originPassword = environment.getProperty(DS_KEY) ;
        System.out.println("原始密码： " + originPassword) ;
        if (originPassword.startsWith(PWD_PREFIX) && originPassword.endsWith(PWD_SUFFIX)) {
            String encryptPassword = originPassword.substring(originPassword.indexOf('(') + 1, originPassword.length() - 1) ;
            System.out.println(encryptPassword);
//            BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//            textEncryptor.setPassword("admin");
//            String decryptPassword = textEncryptor.decrypt(encryptPassword);

            SM2Util sm2 = new SM2Util();
            String decryptPassword = sm2.decrypt(Base64.getDecoder().decode(encryptPassword), PrivateKey);
//            String decryptPassword = decrypt(encryptPassword) ;
            System.out.println("解密后的密码：" + decryptPassword) ;
            Map<String, Object> kv = new HashMap<>(2);
            kv.put(DS_KEY, decryptPassword) ;
            PropertySource<Map<String, Object>> source = new MapPropertySource("ds-pwd", kv) ;
            environment.getPropertySources().addFirst(source) ;
        }
        System.out.println("加解密数据处理完成") ;
    }

    private static String decrypt(String source) {
        byte[] bs = source.getBytes(Charset.forName("UTF-8"));
        String s = ByteUtil.toHexString2(bs);
        System.out.println("s = " + s);
        for (int i = 0; i < bs.length; i++) {
            System.out.println("i = " + i);
            System.out.println(ByteUtil.toHexString1(bs[i]));
            int i1 = bs[i] ^ 0x01;
            System.out.println("i1 = " + i1);
            System.out.println("(byte)i1 = " + ByteUtil.toHexString1((byte)i1));
            bs[i] = (byte) (bs[i] ^ 0x01);
        }
        return new String(bs, Charset.forName("UTF-8"));
    }

    public static void main(String[] args) {
        System.out.println(68 ^ 0x01);
        String a = "chongshi";
        String decrypt = decrypt(a);
        System.out.println(decrypt);
        String decrypt1 = decrypt(decrypt);
        System.out.println(decrypt1);


    }


}
